package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.AppUpdateBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.AppUpdatePresenter;
import com.android.infantschool.model.mvp.presenter.AppUpdatePresenterImpl;
import com.android.infantschool.model.mvp.view.AppUpdateView;
import com.android.infantschool.ui.dialog.CommomDialog;
import com.android.infantschool.util.SystemUtil;
import com.android.infantschool.util.ToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetUpActivity extends BaseActivity {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.tv_exit)
    public TextView tvExit;
    @BindView(R.id.tv_system_notice)
    public TextView tvSystemNotice;
    @BindView(R.id.tv_help)
    public TextView tvHelp;
    @BindView(R.id.tv_feedback)
    public TextView tvFeedback;
    @BindView(R.id.tv_about)
    public TextView tvAbout;
    @BindView(R.id.tv_update_version)
    public TextView tvUpdateVersion;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, SetUpActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("设置");

        Drawable drawable = getResources().getDrawable(R.mipmap.icon_you);
        // 设置图片的大小
        drawable.setBounds(0, 0, 40, 40);
        // 设置图片的位置，左、上、右、下
        tvSystemNotice.setCompoundDrawables(null, null, drawable, null);
        tvHelp.setCompoundDrawables(null, null, drawable, null);
        tvFeedback.setCompoundDrawables(null, null, drawable, null);
        tvAbout.setCompoundDrawables(null, null, drawable, null);
        tvUpdateVersion.setCompoundDrawables(null, null, drawable, null);
        tvUpdateVersion.setText("版本更新  v" + String.valueOf(SystemUtil.getVersionName()));
    }

    @OnClick({R.id.img_back, R.id.tv_exit, R.id.tv_system_notice, R.id.tv_help, R.id.tv_feedback, R.id.tv_about, R.id.tv_update_version})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_exit:
                new CommomDialog(this, R.style.dialog_loading, "确认退出吗？", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            dialog.dismiss();
                            AppConfig.setLoginStatus(false);
                            startActivity(LoginActivity.newIntent(SetUpActivity.this));
                            SetUpActivity.this.finish();
                            if (MainActivity.Instance != null) {
                                MainActivity.Instance.finish();
                            }
                        }
                    }
                }).setTitle("提示").show();
                break;
            case R.id.tv_system_notice:
                startActivity(SystemActivity.newIntent(this));
                break;
            case R.id.tv_help:
                break;
            case R.id.tv_feedback:
                startActivity(FeedBackActivity.newIntent(this));
                break;
            case R.id.tv_about:
                startActivity(AboutActivity.newIntent(this));
                break;
            case R.id.tv_update_version:
                this.showLoading();
                checkApp();
                break;
        }
    }

    private void checkApp() {
        AppUpdatePresenter updatePresenter = new AppUpdatePresenterImpl(new AppUpdateView() {
            @Override
            public void errorAppUpdate(String error) {
                SetUpActivity.this.dismissLoading();
                ToastUtil.showShortToast(SetUpActivity.this, "网络出现异常:" + error);
            }

            @Override
            public void successAppUpdate(BaseBean baseBean) {
                SetUpActivity.this.dismissLoading();
                AppUpdateBean updateBean = (AppUpdateBean) baseBean;
                if (updateBean.getCode() == 0) {
                    if (SystemUtil.getVersionCode() < updateBean.getData().getVercode()) {
                        ToastUtil.showShortToast(SetUpActivity.this, "发现新版本:" + updateBean.getData().getVerName());
                    } else {
                        ToastUtil.showShortToast(SetUpActivity.this, "当前已是最新版本");
                    }
                } else {
                    ToastUtil.showShortToast(SetUpActivity.this, "检测失败:" + updateBean.getMsg());
                }
            }
        });
        Map<String, String> map = new HashMap<>();
        updatePresenter.getAppUpdate(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}